package com.zhijian.medical.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhijian.medical.dao.local.RoutineSampleArchiveMapper;
import com.zhijian.medical.entity.po.RoutineSampleArchive;
import com.zhijian.medical.entity.vo.request.RoutineSampleArchiveSearchVo;
import com.zhijian.medical.entity.vo.response.RoutineSampleArchiveVo;
import com.zhijian.medical.util.AssertUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * 归档的业务处理
 */
@Service
public class RoutineSampleArchiveService {

    @Autowired
    RoutineSampleArchiveMapper routineSampleArchiveMapper;

    /**
     * 添加归档信息
     * @param routineSampleArchive 归档信息
     * @return
     */
    public void addRoutineSampleArchive(RoutineSampleArchive routineSampleArchive){
        Date now = new Date();
        QueryWrapper<RoutineSampleArchive> queryWrapper = new QueryWrapper<>();
        for (Long sampleId : routineSampleArchive.getSampleIdList()){
            queryWrapper.eq("sample_id", sampleId);
            queryWrapper.eq("archive_type", routineSampleArchive.getArchiveType());
            RoutineSampleArchive old = routineSampleArchiveMapper.selectOne(queryWrapper);
            routineSampleArchive.setUpdateTime(now);
            if (old != null){
                routineSampleArchive.setId(old.getId());
                routineSampleArchive.setState(1);
                routineSampleArchiveMapper.updateById(routineSampleArchive);
            }else {
                routineSampleArchive.setCreateTime(now);
                RoutineSampleArchive copy = new RoutineSampleArchive();
                BeanUtils.copyProperties(routineSampleArchive, copy);
                copy.setSampleId(sampleId);
                copy.setState(1);
                routineSampleArchiveMapper.insert(copy);
            }
        }
    }

    public com.zhijian.util.page.Page<RoutineSampleArchiveVo> selectPageList(RoutineSampleArchiveSearchVo searchVo){
        //处理请求参数
        handleReqParam(searchVo);
        Page<RoutineSampleArchiveVo> dataList;
        if (ArchiveType.QP.getCode().equals(searchVo.getArchiveType())){
            PageHelper.startPage(searchVo.getPageNum(), searchVo.getPageSize());
            dataList = routineSampleArchiveMapper.selectQpPageList(searchVo);
        }else {
            PageHelper.startPage(searchVo.getPageNum(), searchVo.getPageSize());
            dataList = routineSampleArchiveMapper.selectLkPageList(searchVo);
        }
        if (CollectionUtil.isNotEmpty(dataList.getResult())){
            for (RoutineSampleArchiveVo routineSampleArchiveVo : dataList.getResult()){
                String number = routineSampleArchiveVo.getNumber();
                if (StringUtils.hasText(number)){
                    String[] numArr = number.split("-");
                    routineSampleArchiveVo.setConventionPathologyNumber(numArr[0]);
                }
                if (routineSampleArchiveVo.getArchiveType() == null){
                    routineSampleArchiveVo.setArchiveType(0);
                }
            }
        }
        com.zhijian.util.page.Page<RoutineSampleArchiveVo> result =
                new com.zhijian.util.page.Page<>(true,
                        Integer.parseInt( String.valueOf(dataList.getTotal())), searchVo.getPageNum(), searchVo.getPageSize());
        result.setData(dataList.getResult());
        return result;
    }

    private void handleReqParam(RoutineSampleArchiveSearchVo searchVo){
        String blhQuery = searchVo.getConventionPathologyNumber();
        String numberQuery = searchVo.getNumber();
        if (StringUtils.hasText(blhQuery)){
            if (blhQuery.contains("~")){
                String[] val = blhQuery.split("~");
                Set<String> blhList = new LinkedHashSet<>();
                AssertUtil.check(val.length != 2, "病理号查询条件异常");
                String prefix = val[0].substring(0, val[0].length() - 5);
                int startIndex = Integer.parseInt(val[0].substring(val[0].length() - 5));
                int endIndex = Integer.parseInt(val[1]);
                int i = 0;
                for ( ;startIndex <= endIndex; startIndex++){
                    String code = org.apache.commons.lang3.StringUtils.leftPad(String.valueOf(startIndex), 5 ,"0");
                    blhList.add(prefix + code);
                    i++;
                    if (i>100){
                        break;
                    }
                }
                searchVo.setBlhList(new LinkedList<>(blhList));
            }else {
                List<String> blhList = new LinkedList<>();
                blhList.add(blhQuery);
                searchVo.setBlhList(blhList);
            }
        }
        if (StringUtils.hasText(numberQuery)){
            if (numberQuery.contains("~")){
                String[] val = numberQuery.split("~");
                Set<String> blhList = new LinkedHashSet<>();
                AssertUtil.check(val.length != 2, "病理号查询条件异常");
                String prefix = val[0];
                String newPrefix = prefix.substring(0, prefix.lastIndexOf("-"));
                int startIndex = Integer.parseInt(prefix.substring(prefix.lastIndexOf("-") + 1));
                int endIndex = Integer.parseInt(val[1]);
                int i = 0;
                for ( ;startIndex <= endIndex; startIndex++){
                    blhList.add(newPrefix + "-" + startIndex);
                    i++;
                    if (i>100){
                        break;
                    }
                }
                searchVo.setQphList(new LinkedList<>(blhList));
            }else {
                List<String> numberList = new LinkedList<>();
                numberList.add(numberQuery);
                searchVo.setQphList(numberList);
            }
        }
    }

    @Getter
    @AllArgsConstructor
    public enum ArchiveType{

        /**
         * 切片
         */
        QP(0),

        /**
         * 蜡块
         */
        LK(1);

        private Integer code;

    }
}
